package com.example.demo.mqsever.core;

import com.example.demo.common.ConsumerEnv;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

// 存储消息的队列
public class MSGQueue {
    private String name;
    // 持久化参数
    private boolean durable = false;
    // 表示该队列是否只能被一个人使用，未实现
    private boolean exclusive = false;
    // 自动删除，此处未使用，如Exchange
    private boolean autoDelete = false;
    private Map<String,Object> arguments = new HashMap<>();
    // 当前队列被哪些消费者订阅
    private List<ConsumerEnv> consumerEnvList = new ArrayList<>();
    // 记录当前取到了第几个消费者，方便实现轮询政策
    // 避免线程安全问题，利用原子类型操作
    private AtomicInteger consumerSeq = new AtomicInteger(0);

    // 添加订阅者
    public void addConsumerEnv(ConsumerEnv consumerEnv){
        consumerEnvList.add(consumerEnv);
    }

    // 挑选订阅者来处理当前的消息（轮询方式）
    public ConsumerEnv chooseConsumer(){
        if (consumerEnvList.size() == 0){
            return null; // 没有人订阅该队列
        }
        int index = consumerSeq.get() % consumerEnvList.size();
        consumerSeq.getAndIncrement();
        return consumerEnvList.get(index);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

//    public Map<String, Object> getArguments() {
//        return arguments;
//    }
//
//    public void setArguments(Map<String, Object> arguments) {
//        this.arguments = arguments;
//    }

    public String getArguments(){
        // Map 类型转换为 String（json）
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return "{}";
    }

    public void setArguments(String arguments){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            this.arguments = objectMapper.readValue(arguments, new TypeReference<HashMap<String, Object>>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public void setArguments(Map<String,Object> arguments){
        this.arguments = arguments;
    }

    // 用于 java 内部代码调用（例如测试类）
    public Object getArguments(String key){
        return arguments.get(key);
    }

    public void setArguments(String key, Object value){
        arguments.put(key,value);
    }
}
